<template>
  <div>
    <f-table ref='table' rowKey='orderDetailsId' :scroll='scroll' :showSetting='true' setting-key='iv1' :tableColumns='columns'
             handler='noInvoiceDetailsPageListHandler' :showMultipleBtn='false' :multiple='multiple'
             :operator='operator' :params='queryParam' :api='api.bod'>
     <span slot='action' slot-scope='text, record'>
          <template>
             <a @click='$refs.orderInfoDrawer.showDrawer(record)'>查看</a>
          </template>
        </span>
      <span slot="supplierRetable" slot-scope="text, record, index">
            <a-tag color="#f50" v-if="record.kaipiaoId!=''" title='已开票'>{{text}}</a-tag>
            <span v-else>{{text}}</span>
        </span>
      <span slot="supplierRetable2" slot-scope="text, record, index">
            <a-tag color="#f50" v-if="record.kaipiaoId2!=''" title='已开票'>{{text}}</a-tag>
            <span v-else>{{text}}</span>
        </span>
    </f-table>
    <invoice-drawer ref='invoiceDrawer' @success='onLoad'></invoice-drawer>
    <order-info-drawer ref='orderInfoDrawer'></order-info-drawer>
  </div>
</template>
<script>

import api from "./../js/api"
import InvoiceDrawer from "./invoice_drawer"
import OrderInfoDrawer from "@/views/module/business/order/order_info_drawer"

export default {
  name: "InvoiceNo",
  components: { OrderInfoDrawer, InvoiceDrawer },
  data() {
    let me = this
    //操作按钮
    let operator = []
    let companyId = this.$store.getters.companyId
    operator.push({
      name: "开票申请",
      icon: "transaction",
      that: this,
      handler: function(table, that) {
        let selectIds = table.selectedRowKeys
        if (selectIds.length > 0) {
          let supplierRetable = table.getSelectedTotal("supplierRetable")
          me.$refs["invoiceDrawer"].showDrawer({
            supplierRetable: supplierRetable,
            selectIds: selectIds.join(","),
            supplierId: me.supplierId
          }, "form1")
        } else {
          if (!that.queryParam.supplierId) {
            that.$message.error("请选择供货方")
            return
          }
          that.$message.error("请选择开票明细")
        }
      }
    })
    if (companyId == 4) {
      operator.push({
        name: "真奥医贸开票申请",
        icon: "transaction",
        that: this,
        handler: function(table, that) {
          let selectIds = table.selectedRowKeys
          if (selectIds.length > 0) {
            let supplierRetable = table.getSelectedTotal("supplierRetable2")
            me.$refs["invoiceDrawer"].showDrawer({
              supplierRetable: supplierRetable,
              selectIds: selectIds.join(","),
              supplierId: me.supplierId
            }, "form4")
          } else {
            if (!that.queryParam.supplierId) {
              that.$message.error("请选择供货方")
              return
            }
            that.$message.error("请选择开票明细")
          }
        }
      })
    }

    let columns=[
      {
        title: "序号",
        width: "60px",
        fixed: "left",
        align: "left",
        customRender: (text, record, index) => index + 1
      }, {
        title: "操作",
        align: "left",
        dataIndex: "action",
        width: "100px",
        fixed: "left",
        scopedSlots: { customRender: "action" }
      },
      {
        title: "单号",
        align: "center",
        width: "140px",
        fixed: "left",
        dataIndex: "orderNo"
      },
      {
        title: "部门",
        align: "left",
        width: "100px",
        dataIndex: "deptName"
      },
      {
        title: "业务员",
        align: "left",
        width: "80px",
        dataIndex: "saleManName"
      },
      {
        title: "发货金额",
        align: "left",
        width: "80px",
        dataIndex: "saleAmount"
      },
      {
        title: "发货日期",
        align: "left",
        width: "100px",
        sorter: true,
        dataIndex: "sendDate"
      },
      {
        title: "供货方",
        align: "left",
        width: "170px",
        ellipsis: true,
        dataIndex: "supplierName"
      },
      {
        title: "购货方",
        align: "left",
        width: "170px",
        ellipsis: true,
        dataIndex: "customName"
      },
      {
        title: "商品名称",
        align: "left",
        width: "130px",
        ellipsis: true,
        dataIndex: "drugsName"
      },
      {
        title: "规格",
        align: "left",
        width: "100px",
        dataIndex: "drugsSpec"
      },
      {
        title: "数量",
        align: "left",
        width: "100px",
        dataIndex: "saleNum"
      },
      {
        title: "单价",
        align: "left",
        width: "100px",
        dataIndex: "salePrice"
      },
      {
        title: "回款金额",
        align: "left",
        width: "100px",
        dataIndex: "returnAmount"
      },
      {
        title: "回款日期",
        align: "left",
        width: "100px",
        dataIndex: "returnDate"
      },
      {
        title: "应返金额",
        align: "left",
        fixed: "right",
        needTotal: true,
        width: "100px",
        scopedSlots: { customRender: "supplierRetable" },
        dataIndex: "supplierRetable"

      }
    ];
    if(companyId==4){
      columns.push({
        title: "应返金额2",
        align: "left",
        fixed: "right",
        needTotal: true,
        scopedSlots: { customRender: "supplierRetable2" },
        width: "100px",
        dataIndex: "supplierRetable2"
      })
    }

    return {
      api: Object.assign(api, this.api),
      multiple: false,
      supplierId: "",
      scroll: { x: true },
      //列表初始化查询参数
      queryParam: {},
      // 表头
      columns: columns,
      operator: operator
    }
  },
  created() {
  },
  methods: {
    setData() {
    },
    /**
     * 查询事件
     * @param searchParams
     */
    searchTable(searchParams) {
      if (searchParams.supplierId) {
        this.multiple = true
        this.$refs.table.openMultiple()
      } else {
        this.multiple = false
        this.$refs.table.closeMultiple()
      }
      this.queryParam = Object.assign(this.queryParam, searchParams)
      this.$refs.table.refresh(true, searchParams)
    },
    openMultiple(searchParams) {
      if (searchParams.supplierId) {
        this.supplierId = searchParams.supplierId
        this.multiple = true
        this.$refs.table.openMultiple()
      } else {
        this.multiple = false
        this.$refs.table.closeMultiple()
      }
    },
    /**
     * 刷新列表
     */
    onLoad() {
      this.$refs.table.refresh()
      this.$emit("changeTab")
    }
  }
}
</script>
